import { Context } from 'koa'
import Router from 'koa-router'
import { success } from '../common/ResResult'
import { UserInfo, userdao } from '../modules/userinfo/dao/UserDao'
// import { userDaoOrm } from '../dao/UserDaoOrm'

const router = new Router()

router.prefix('/usermodule')

router.get('/findOneUser/:username/:psw', async (ctx: Context) => {
  const { username, psw } = ctx.params
  const userinfo = await userdao.findByUsrAndPsw(username, psw)
  console.log('findUserInfo', userinfo)
  ctx.body = success(userinfo)
  // const r = null as any
  // r[0]
  // ctx.body = success(`welcom - ${username}`)
})

router.get('/countUserInfo', async (ctx: Context) => {
  const userinfo = await userdao.countUserInfo()
  console.log('countUserInfo', userinfo)
  ctx.body = success(userinfo)
})

router.get('/findUserWithPager/:pagenum/:pagesize', async (ctx: Context) => {
  const { pagenum, pagesize } = ctx.params
  const offset = (pagenum - 1) * pagesize
  const userinfo = await userdao.findUserWithPager(+pagesize, +offset)
  console.log('findUserWithPager', userinfo)
  ctx.body = success(userinfo)
})

router.get('/findByUsrOrAddr/:key', async (ctx: Context) => {
  const { key } = ctx.params
  const userinfo = await userdao.findByUsrOrAddr(key)
  console.log('findByUsrOrAddr', userinfo)
  ctx.body = success(userinfo)
})

router.get('/findAllUser', async (ctx: Context) => {
  // const r = await userdao.findAllUser()
  const r = await userdao.findAllUser()
  console.log('findAllUser', r)
  ctx.body = success(r)
})

router.get('/findLike/:key', async (ctx: Context) => {
  const { key } = ctx.params
  const r = await userdao.findByLike(key)
  console.log('findByLike', r)
  ctx.body = success(r)
})

router.get('/findByProps', async (ctx: Context) => {
  const r = await userdao.findByProps()
  console.log('findByProps', r)
  ctx.body = success(r)
})

router.post('/addUser', async (ctx: Context) => {
  // const user = ctx.request.body
  // ctx.body = `welcom - ${user.username}`
  const userinfo: UserInfo = ctx.request.body

  const dbUserInfo = await userdao.addUser(userinfo)
  // console.log(dbUserInfo)
  ctx.body = success(dbUserInfo)
})

// export default router
module.exports = router
